package com.ybb.vue.common.util;

import com.ybb.vue.entity.OperationRecords;
import com.ybb.vue.service.OperationRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ybb
 * @version 1.0
 * @Description 操作记录工具类
 * @Date 2023/3/30 14:34
 */

@Component
public class OperationRecordsUtils {

    @Autowired
    private OperationRecordsService operationRecordsService;

    private static OperationRecordsService myOperationRecordsService;

    @PostConstruct
    public void init() {
        myOperationRecordsService = operationRecordsService;
    }

    /**
     *  生成操作记录，操作内容两个参数
     * @param operateContent
     */
    public static void createOperationRecords(String operateContent){
        OperationRecords operationRecords = new OperationRecords();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String operater = SecurityContextHolder.getContext().getAuthentication().getName();
        String content = "【" + operater + "】于【" + sdf.format(date) + "】" + operateContent;
        operationRecords.setOperateContent(content);
        operationRecords.setOperateTime(date);
        operationRecords.setOperater(operater);
        myOperationRecordsService.save(operationRecords);
    }
}
